﻿using AutoMapper;
using Inventory.API.Application.Command.WarehouseManagementCommand;
using Inventory.Domain;
using Inventory.Errorcode.ErrorCode;
using Inventory.Infrastructure.Interfaces;
using MediatR;

namespace Inventory.API.Application.CommandHandler.WarehouseManagementCommandHandler
{
    public class GetWarehousingInfoCommandHanlder : IRequestHandler<GetWarehousingInfoCommand, ApiResult<Warehousemanagement>>
    {
        private readonly ILogger<GetWarehousingInfoCommandHanlder> _logger;

        private readonly IMapper _mapper;

        private readonly IWarehousemanagementRepository _warehousemanagementRepository;

        public GetWarehousingInfoCommandHanlder(ILogger<GetWarehousingInfoCommandHanlder> logger, IMapper mapper, IWarehousemanagementRepository warehousemanagementRepository)
        {
            _logger = logger;
            _mapper = mapper;
            _warehousemanagementRepository = warehousemanagementRepository;
        }
        public async Task<ApiResult<Warehousemanagement>> Handle(GetWarehousingInfoCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var warehousemanagemen = await _warehousemanagementRepository.GetAsync(x=>x.Warehousemanagementid==request.Warehousemanagementid);
                if (warehousemanagemen == null)
                {
                    return ApiResult<Warehousemanagement>.Fail("未查找id", StatusCodeEnum.NotFound);
                }
                else
                {
                    return ApiResult<Warehousemanagement>.Success(warehousemanagemen, StatusCodeEnum.Success);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error occurred while getting warehousing info");
                throw;
            }
        }
    }
}
